<template>
  <el-cascader
    v-model="selectData.val"
    :options="selectData.cateList"
    :props="selectData.cateProps"
    @change="cascaderChange"
    clearable  
  >
  </el-cascader>
</template>

<script lang="ts">
import { defineComponent, reactive, computed } from 'vue'
import { useStore } from '@/store'

export default defineComponent({
  props: {
    redactOrAdd: {
      type: Boolean,
      required: true
    }
  },
  emits: ['cascader-data'],
  setup(props, context) {
    const store = useStore()
    const selectDataVal = computed({
      get: () => {
        const catArr = store.state.productList.redactModule.redactData.goods_cat?.split(',')
        return catArr?.map(item => {
          return +item
        })
      },
      set: (val: any) => {
        if (val) {
          store.state.productList.redactModule.redactData.goods_cat = Object.values(val).join(',')
        }
      }
    })
    const selectData = reactive({
      val: props.redactOrAdd ? selectDataVal : [],
      cateList: computed(() => store.state.paramsModule.categoriesList), // 商品分类数据获取
      cateProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    })
    const cascaderChange = () => {
      context.emit('cascader-data', selectData.val)
    }
    return { selectData, cascaderChange }
  },
})
</script>
